<?php

namespace App\Model;


use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use mysql_xdevapi\Exception;

class CheckRecord extends BaseModel
{
    protected $connection = 'mysql';
    protected $table = 'check_record';
    protected $primaryKey = 'check_id';
    private $fields = ['check_id','recharge_user_total','recharge_price','real_recharge_price','create_time','updated_at'];
    protected $fillable = ['recharge_user_total','recharge_price','create_time'];
    
    public function getRechargePriceAttribute($value){
        if(!$value) return 0;
        return $value /100;
    }
    
    public function getRealRechargePriceAttribute($value){
        if(!$value) return 0;
        return $value /100;
    }
    
    public function setRealRechargePriceAttribute($value){
        $this->attributes['real_recharge_price'] = $value * 100;
    }
    
    public function getList(Request $request){
        $limit = $request->input('limit',10);
        $page = $request->input('page',1);
        $begin_date = $request->input('begin_date',0);
        $end_date = $request->input('end_date',0);
        
        $query = new self();
        
        if($begin_date && $end_date) $query = $query->whereBetween('create_time', [ $begin_date, $end_date ]);
        if($begin_date && !$end_date) $query = $query->where('create_time','>=', $begin_date);
        if(!$begin_date && $end_date) $query = $query->where('create_time','<=', $end_date);
        
        return $query->orderBy('check_id','desc')
            ->where('recharge_type',2)
            ->paginate($limit,$this->fields,'page',$page);
    }
    
    public function setRealRechargePrice(Request $request, int $check_id){
        $model = CheckRecord::find($check_id);
        $model->real_recharge_price = $request->input('real_recharge_price');
        return $model->save();
    }
}
